package leetcode.code0433;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	char[] a = new char[] { 'A', 'C', 'G', 'T' };

	public int minMutation(String start, String end, String[] bank) {
		int len = bank.length;
		boolean[] been = new boolean[len];
		Map<String, Integer> hash = new HashMap<>();
		for (int i = 0; i < been.length; i++) {
			hash.put(bank[i], i);
		}
		if (!hash.containsKey(end)) {
			return -1;
		}
		Queue<int[]> queue = new LinkedList<>();
		queue.add(new int[] { -1, 0 });
		while (!queue.isEmpty()) {
			int[] cur = queue.poll();
			char[] cs = cur[0] == -1 ? start.toCharArray() : bank[cur[0]].toCharArray();
			int dist = cur[1] + 1;
			for (int i = 0; i < 8; i++) {
				char replace = cs[i];
				for (int j = 0; j < 4; j++) {
					cs[i] = a[j];
					String tmp = new String(cs);
					int idx = hash.getOrDefault(tmp, -1);
					if (idx != -1 && !been[idx]) {
						if (tmp.equals(end)) {
							return dist;
						}
						queue.add(new int[] { idx, dist });
						been[idx] = true;
					}
				}
				cs[i] = replace;
			}
		}
		return -1;
	}

	@Override
	public void debug4() {
		HelpDebug.compare(2,
				this.minMutation("AACCGGTT", "AAACGGTA", HelpDebug.str2array2("[\"AACCGGTA\",\"AACCGCTA\",\"AAACGGTA\"]")));
		HelpDebug.compare(3,
				this.minMutation("AAAAACCC", "AACCCCCC", HelpDebug.str2array2("[\"AAAACCCC\",\"AAACCCCC\",\"AACCCCCC\"]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
